<?php

namespace App\Manage;


use App\Models\Admin;
use App\Models\AdminRole;

class AdminManage extends Manage
{

    // 检查权限

    // 修改

    // 删除


    // 模型
    protected function getModel()
    {
        return new Admin();
    }

    // 整理列表数据
    protected function _afterList($list)
    {
        if(empty($list)){
            return $list;
        }

        $roles = (new AdminRole())->getListCache([]);
        $roles = array_column($roles, null, 'id');
        foreach($list as &$_row){
            $_row['admin_role'] = $roles[$_row['role_id']] ?? [];
            $_row['role_name']  = isset($roles[$_row['role_id']]) ? $roles[$_row['role_id']]['name'] : '';
        }

        return $list;
    }


    // 查找
    public function show($myUser, $options = [])
    {
        $opt = $this->option();
        $id  = $opt->getData('id', 0, 'intval');
        if(empty($id)){
            //return $this->_returnFail('请指定查看记录!');
            return $this->_returnFail('select_operation_id');
        }


        // 参数
        $is_admin_log  = $options['is_admin_log'] ?? 0;
        $is_admin_role = $options['is_admin_role'] ?? 0;


        $where = ['id' => $id];
        $model = new Admin();
        $info  = $model->oneFindCache($where, $where);
        if($info === false){
            return $this->_returnFail($model->getError());
        }

        // 权限整理
        $info = is_object($info) ? $info->toArray() : $info;
        // 权限整理
        $info['password'] = ''; // 方便展示为空
        $info['role']     = json_decode($info['role']);
        if($is_admin_role){
            $info['menu'] = $model->getUserMenu($info['id'], $info['role']);
        }

        // 如果是build-admin
        $isBuild = $opt->getHeader('Build-Admin', 0, 'intval');
        if($isBuild){
            return ['row' => $info];
        }

        return $info;
    }


    // 获取权限
    public function role($myUser, $options = [])
    {
        $roles = (new Admin())->getAllMenu();

        return $roles;
    }

    // 修改密码
    public function password($myUser, $options = [])
    {
        $opt      = $this->option();
        $password = $opt->getData('password', '123456', 'trim');
        $id       = $opt->getData('id', 0, 'intval');
        if(empty($id)){
            //return $this->_returnFail('请指定查看记录!');
            return $this->_returnFail('select_operation_id');
        }


        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        $model = new Admin();
        $data  = ['password' => $password,];
        $data  = $model->checkData($data, ['id' => $id,]);
        if($data === false){
            return $this->_returnFail($model->getError());
        }


        $where  = $this->editWhere();
        $result = $model->oneEdit($where, $data);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        // 管理员日志
        if($is_admin_log){
            //$this->addAdminLog($myUser['id'], '修改的密码', '');
        }


        return true;
    }


    // 启用禁用
    public function status($myUser, $options = [])
    {

        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        $opt    = $this->option();
        $id     = $opt->getData('id', 0, 'intval');
        $_value = $opt->getData('status', 0, 'intval');
        if(empty($id)){
            //return $this->_returnFail('请指定查看记录！');
            return $this->_returnFail('select_operation_id');
        }
        if($id == 1){
            //return $this->_returnFail('第一个管理员不能被禁用！！');
            return $this->_returnFail('first_administrator_cannot_be_disabled');
        }
        if(is_null($_value)){
            //return $this->_returnFail('是否启用状态必须填写！！');
            return $this->_returnFail('enable_disable_status_must_be_filled');
        }
        if(!in_array($_value, [0, 1, -1])){
            //return $this->_returnFail('是否启用状态必须为0或1！！');
            return $this->_returnFail('status_value_must_be_0_1');
        }

        // 执行
        $model  = new Admin();
        $result = $model->oneEdit(['id' => $id], ['status' => $_value]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }


        // 管理员日志
        if($is_admin_log){
            //$this->addAdminLog($myUser['id'], '修改自己的个人资料', '');
        }


        return true;
    }

    // 添加
    protected function _before_add()
    {

        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        $opt  = $this->option();
        $data = [
            'nickname' => $opt->getData('nickname', null, 'trim'),
            'username' => $opt->getData('username', null, 'trim'),
            'password' => $opt->getData('password', '', 'trim'),
            'mobile'   => $opt->getData('mobile', null, 'trim'),
            'role'     => $opt->getData('role', null, 'is_array'),
            'role_id'  => $opt->getData('role_id', null, 'intval'),
            'status'   => $opt->getData('status', 1, 'intval'),
        ];

        if($data['role']){
            if(count($data['role']) != count($data['role'], 1)){
                //return $this->_returnFail('权限必须是一维数组！');
                return $this->_returnFail('permissions_must_one_dimensional_array');
            }
        }


        // 检查唯一性
        $model = new Admin();
        $data  = $model->checkUser($data);
        if($data === false){
            return $this->_returnFail($model->getError());
        }
        $data  = $model->checkData($data);
        if($data === false){
            return $this->_returnFail($model->getError());
        }
        // 管理员日志
        if($is_admin_log){
            //$this->addAdminLog($myUser['id'], '修改自己的个人资料', '');
        }


        return $data;
    }

    // 修改
    protected function _before_edit()
    {

        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        $opt  = $this->option();
        $id   = $this->option()->getData('id', 0, 'intval');
        $data = [
            'username' => $opt->getData('username', null, 'trim'),
            'nickname' => $opt->getData('nickname', null, 'trim'),
            'password' => $opt->getData('password', '', 'trim'),
            'mobile'   => $opt->getData('mobile', null, 'trim'),
            'role'     => $opt->getData('role', null, 'is_array'),
            'role_id'  => $opt->getData('role_id', null, 'intval'),
            'status'   => $opt->getData('status', 1),
        ];


        if($data['role']){
            if(count($data['role']) != count($data['role'], 1)){
                //return $this->_returnFail('权限必须是一维数组！');
                return $this->_returnFail('permissions_must_one_dimensional_array');
            }
        }

        // 检查唯一性
        $model = new Admin();
        $data  = $model->checkUser($data, ['id' => $id,]);
        if($data === false){
            return $this->_returnFail($model->getError());
        }
        $data  = $model->checkData($data, ['id' => $id,]);
        if($data === false){
            return $this->_returnFail($model->getError());
        }

        // 管理员日志
        if($is_admin_log){
            //$this->addAdminLog($myUser['id'], '修改自己的个人资料', '');
        }


        return $data;
    }
}

